package com.qiaomeng.flutter.baichuan.handlers;


import android.app.Application;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;

import com.alibaba.triver.kit.api.proxy.IImageProxy;
import com.facebook.common.executors.UiThreadImmediateExecutorService;
import com.facebook.common.internal.Preconditions;
import com.facebook.common.references.CloseableReference;
import com.facebook.datasource.BaseDataSubscriber;
import com.facebook.datasource.DataSource;
import com.facebook.datasource.DataSubscriber;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.DraweeView;
import com.facebook.imagepipeline.core.ImagePipeline;
import com.facebook.imagepipeline.image.CloseableImage;
import com.facebook.imagepipeline.image.CloseableStaticBitmap;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.imagepipeline.request.ImageRequest;

public class ImageImpl implements IImageProxy {
    @Override
    public void setImageUrl(final ImageView imageView, String s, ImageStrategy imageStrategy) {
        ImageRequest request = ImageRequest.fromUri(s);

        if (imageView instanceof DraweeView) {
            ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
                @Override
                public void onFailure(String id, Throwable throwable) {
                    super.onFailure(id, throwable);
                }

                @Override
                public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                    super.onFinalImageSet(id, imageInfo, animatable);
                }

                @Override
                public void onIntermediateImageSet(String id, ImageInfo imageInfo) {
                    super.onIntermediateImageSet(id, imageInfo);
                }
            };

            DraweeController controller = Fresco.newDraweeControllerBuilder().setAutoPlayAnimations(true).setControllerListener(controllerListener).setUri(request.getSourceUri()).setImageRequest(request).build();
            ((DraweeView) imageView).setController(controller);
        } else {
            ImagePipeline imagePipeline = Fresco.getImagePipeline();
            DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(request, new Object());
            DataSubscriber dataSubscriber = new BaseDataSubscriber<CloseableReference<CloseableImage>>() {
                @Override
                protected void onNewResultImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
                    CloseableReference<CloseableImage> imageReference = (CloseableReference) dataSource.getResult();
                    if (imageReference != null) {
                        try {
                            Preconditions.checkState(CloseableReference.isValid(imageReference));
                            CloseableImage closeableImage = (CloseableImage) imageReference.get();
                            if (!(closeableImage instanceof CloseableStaticBitmap)) {
                                throw new UnsupportedOperationException("Unrecognized image class: " + closeableImage);
                            }

                            CloseableStaticBitmap closeableStaticBitmap = (CloseableStaticBitmap) closeableImage;
                            imageView.setImageBitmap(closeableStaticBitmap.getUnderlyingBitmap());
                        } finally {
                            imageReference.close();
                        }
                    }
                }

                @Override
                protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {

                }
            };
            dataSource.subscribe(dataSubscriber, UiThreadImmediateExecutorService.getInstance());
        }
    }

    @Override
    public void loadImage(String s, ImageStrategy imageStrategy, final ImageListener imageListener) {
        if (!TextUtils.isEmpty(s)) {
            Uri uri = Uri.parse(s);
            ImageRequest request = ImageRequest.fromUri(s);
            ImagePipeline imagePipeline = Fresco.getImagePipeline();
            DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(request, new Object());
            DataSubscriber dataSubscriber = new BaseDataSubscriber<CloseableReference<CloseableImage>>() {
                public void onNewResultImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
                    CloseableReference<CloseableImage> imageReference = (CloseableReference) dataSource.getResult();
                    if (imageReference != null) {
                        try {
                            Preconditions.checkState(CloseableReference.isValid(imageReference));
                            CloseableImage closeableImage = (CloseableImage) imageReference.get();
                            if (!(closeableImage instanceof CloseableStaticBitmap)) {
                                throw new UnsupportedOperationException("Unrecognized image class: " + closeableImage);
                            }

                            CloseableStaticBitmap closeableStaticBitmap = (CloseableStaticBitmap) closeableImage;
                            if (imageListener != null) {
                                imageListener.onImageFinish(new BitmapDrawable(closeableStaticBitmap.getUnderlyingBitmap()));
                            }
                        } finally {
                            imageReference.close();
                        }
                    }

                }

                public void onFailureImpl(DataSource dataSource) {
                    Log.w("onFailureImpl", "fail");
                }
            };
            dataSource.subscribe(dataSubscriber, UiThreadImmediateExecutorService.getInstance());
        }
    }
}
